using UnityEngine;
using QFramework;
using YooAsset;
using System.IO;
using System.Collections.Generic;
using YooAsset.Editor;


/**
 * 登录页面资源更新加载参考流程~
 */
public class QFYooassetModule : AbstractModel
{
    // !这里的变量是资源释放管理，实际是全局变量，可以放到其他地方，分离该类
    public List<AssetHandle> massetHandles = new List<AssetHandle>();
    public List<ResourcePackage> mResourcePackage = new List<ResourcePackage>();
    protected override void OnInit()
    {
        Debug.Log("Yoo被初始化");
        // 初始化资源系统
        YooAssets.Initialize();
        // 添加创建器加载模板与初始化接入yoo资源管理
        ResKit.Init();
        //将Yoo加载加入UI对象池
        UIKit.Config.PanelLoaderPool = new QFYooAssetsPanelLoaderPool();
        //讲Yoo加入对象池
        AudioKit.Config.AudioLoaderPool = new QFYooAssetsAudioLoaderPool();
        //将Yooasset加入到资源加载工厂
        ResFactory.AddResCreator<QFYooAssetResCreator>();
    }

    #region 释放资源
    public void UnloadUnusedAssetOnChangeSence()
    {
        Debug.Log("开始释放AssetHandle");
        foreach (AssetHandle mHandles in massetHandles)
        {
            mHandles.Release();
        }
        massetHandles.Clear();
        Debug.Log("开始释放mPackage");
        foreach (ResourcePackage mPackage in mResourcePackage)
        {
            mPackage.UnloadUnusedAssetsAsync();
        }
        mResourcePackage.Clear();

    }
    public void Release() 
    {
        Debug.Log("开始释放AssetHandle");
        foreach (AssetHandle mHandles in massetHandles)
        {
            mHandles.Release();
        }
        massetHandles.Clear();
        Debug.Log("开始释放mPackage");
        foreach (ResourcePackage mPackage in mResourcePackage)
        {
            mPackage.UnloadUnusedAssetsAsync();
        }
        mResourcePackage.Clear();
    }
    #endregion

}